package com.heshida.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.heshida.service.AliPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service
public class AliPayServiceImpl implements AliPayService {
    @Autowired
    AlipayClient alipayClient;

    public Map createNative(String out_trade_no, String total_fee) {
        Map<String,String> map=new HashMap();
        AlipayTradePrecreateRequest request=new AlipayTradePrecreateRequest();
        //long total=Long.parseLong(total_fee);
       // BigDecimal bigYuan=BigDecimal.valueOf(total);
        double total=Double.parseDouble(total_fee);
        System.out.println("支付的金额:"+total);
        request.setBizContent("{" +
                "    \"out_trade_no\":\""+out_trade_no+"\"," +
                "    \"total_amount\":\""+total+"\"," +
                "    \"subject\":\"测试购买商品001\"," +
                "    \"store_id\":\"xx_001\"," +
                "    \"timeout_express\":\"120m\"}");//设置业务参数
        try {
            AlipayTradePrecreateResponse response=alipayClient.execute(request);
            //取出响应结果
            String code=response.getCode();
            System.out.println("response:"+code);
            String body=response.getBody();
            System.out.println("response body:"+body);
            if(code.equals("10000")){
                map.put("qrcode",response.getQrCode());
                map.put("out_trade_no",response.getOutTradeNo());
                map.put("total_fee",total_fee);
                System.out.println("qrcode"+response.getQrCode());
                System.out.println("out_trade_no"+response.getOutTradeNo());
                System.out.println("total_fee"+total_fee);

            }else{
                System.out.println("error:"+body);
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return map;
    }

    public Map queryPayStatus(String out_trade_no) {
        Map<String,String> map=new HashMap();
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizContent("{" +
                "    \"out_trade_no\":\""+out_trade_no+"\"," +
                "    \"trade_no\":\"\"}"); //设置业务参数
        try {
            AlipayTradeQueryResponse response = alipayClient.execute(request);
            String code=response.getCode();
            System.out.println("code:"+code);
            System.out.println(response.getBody());
            if(code.equals("10000")){
                map.put("out_trade_no",out_trade_no); //商家的订单号
                map.put("tradestatus",response.getTradeStatus());
                map.put("trade_no",response.getTradeNo()); //支付宝的订单号
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return map;
    }
}
